<template>
	<view class="search">
		<u-search placeholder="请输入搜索关键词" v-model="searchVal" @search="conSearch" @custom="conSearch" :focus="true"
			height="80rpx" shape="square"></u-search>
		<view class="head" v-if="showHistroy">
			<view class="t">搜索历史</view>
			<view class="x" @click="clearLog"><i class="iconfont icon-lajitong"></i></view>
		</view>
		<view class="searchLog" v-if="showHistroy">
			<view class="keys" v-for="(v,i) in searchHistory" @click="goSearch(v)">
				{{v}}
			</view>
		</view>
		<view class="searchList">
			<view style="margin:200rpx 0;" v-if="loading">
				<u-loading-icon mode="semicircle"></u-loading-icon>
			</view>
			<view class="searchData-list" v-else>
				<view class="searchData-list-item" v-for="(v,i) in searchList" @click="goView(v.wid)">
					<view class="title">{{v.title}}</view>
					<view class="t">{{$u.timeFormat(v.create_time, 'yyyy-mm-dd hh:MM:ss')}}</view>
				</view>
				<view class="moreLoading" style="margin:40rpx 0;" v-if="moreLoading">
					<u-loading-icon mode="semicircle"></u-loading-icon>
				</view>
			</view>

		</view>
	</view>
</template>


<script>
	
	import {
		doSearch
	} from '@/api/api.js';
	export default {
		
		data() {
			return {
				searchVal: '',
				searchHistory: '',
				firstOnload: true,
				showHistroy: true,
				searchList: [],
				loading: false,
				moreLoading: false,
				dataPage: {
					'last_page': 0,
					'current_page': 0
				}
			}
		},
		onLoad(options) {
			let _this = this
			if (!uni.getStorageSync('searchList')) {
				uni.setStorageSync('searchList', '[]')
			} else {
				this.searchHistory = JSON.parse(uni.getStorageSync('searchList'))
			}
			// wx.getClipboardData({
			// 	success: function (res) {
			// 		uni.hideToast()
			// 		if(res.data){
			// 			_this.getWebUrl(res.data)
			// 		}
			// 	}
			// });
		},
		watch: {
			searchVal(e) {
				if (e.length <= 0) {
					this.conSearch()
					this.showHistroy = true
					this.loading = false
				}
			}
		},
		onReachBottom() {
			let _this = this
			if (this.dataPage.last_page > this.dataPage.current_page) {
				if (!_this.moreLoading) {
					_this.moreLoading = true;
					_this.goDoSearch(_this.searchVal, parseInt(_this.dataPage.current_page) + 1)
				}
			} else {
				_this.moreLoading = false;
			}
		},
		methods: {
			getWebUrl(string){
				var regex = /wzinfo\?wid\=(\d+)/;
				var result = regex.exec(string);
				if(result[1]){
					uni.showModal({
						content:"识别到剪切板留言板帖文，是否进入？",
						cancelText:'忽略',
						confirmText:'立即进入',
						success(r) {
							if(r.cancel===true){
								uni.setClipboardData({
									data:'',
									showToast:false,
									success() {
										uni.hideToast()
									}
								})
							}else if(r.confirm===true){

								uni.navigateTo({
									url: "/pages/index/thread?wid=" + result[1]
								})
							}
							
						}
					})
				}
			},
			goView(wid) {
				uni.navigateTo({
					url: "/pages/index/thread?wid=" + wid
				})
			},
			clearLog() {
				uni.setStorageSync('searchList', '[]')
				this.searchHistory = ''
			},
			goDoSearch(v, p) {
				let _this = this

				_this.showHistroy = false
				doSearch({
					keywords: v ? v : _this.searchVal,
					page: p ? p : _this.dataPage.current_page
				}).then(r => {
					_this.loading = false
					_this.moreLoading = false
					if (r.data.code == 200) {
						_this.searchList = p == 1 ? r.data.data.data : _this.searchList.concat(r.data.data.data)
						_this.dataPage = {
							'last_page': r.data.data.last_page,
							'current_page': r.data.data.current_page
						}
					}
				})
			},
			goSearch(v) {
				this.searchVal = v
				this.default()
				this.conSearch(v)
			},
			default(){
				this.loading = true
				this.dataPage = {
					'last_page': 0,
					'current_page': 0
				}
				this.searchList = [];
			},
			conSearch(v) {
				this.default()
				this.searchKys(v)
			},
			searchKys() {
				if (this.searchVal) {
					this.goDoSearch(this.searchVal)
					let SsearchHistory = JSON.parse(uni.getStorageSync('searchList') || []);
					SsearchHistory.unshift(this.searchVal);
					let historyArr = new Set(SsearchHistory)
					historyArr = Array.from(historyArr).slice(0, 10)
					this.searchHistory = historyArr
					uni.setStorageSync('searchList', JSON.stringify(historyArr))
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.searchData-list {
		margin-top: 30rpx;
	}

	.searchData-list-item {
		padding: 20rpx 0;
		border-bottom: 1px solid #eee;

		.title {
			font-size: 38rpx;
			color: #000
		}

		.t {
			margin-top: 6rpx;
			font-size: 28rpx;
			color: #999;
		}
	}

	.search {
		margin-top: 40rpx;
		padding: 0 40rpx;

		.head {
			margin: 30rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.t {
				font-size: 32rpx;
				font-weight: bold;
			}

			.iconfont {
				font-size: 50rpx;
			}
		}

		.searchLog {
			margin: 30rpx 0;
			display: flex;
			flex-wrap: wrap;

			.keys {
				margin: 0 16rpx 16rpx 0;
				background-color: #f8f8f8;
				padding: 8rpx 20rpx;
				border-radius: 10rpx;
				color: #777;
				font-size: 30rpx;
			}
		}
	}
</style>